package com.regentsoft.cloudback.enterpriseAdmin.service.impl;

import com.regentsoft.cloudback.enterpriseAdmin.dao.CompanyDao;
import com.regentsoft.cloudback.enterpriseAdmin.entity.ChannelInfo;
import com.regentsoft.cloudback.enterpriseAdmin.entity.CompanyInfo;
import com.regentsoft.cloudback.enterpriseAdmin.service.CompanyInfoService;
import com.regentsoft.cloudback.enterpriseAdmin.vo.ResultResp;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.ChannelAddInfo;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.ChannelDeleteReq;
import com.regentsoft.cloudback.enterpriseAdmin.vo.req.ChannelUpdateReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by work on 2017/11/27.
 *
 * @author lumr
 */
@Service
public class CompanyInfoServiceImpl implements CompanyInfoService {
    @Autowired
    private CompanyDao dao;

    @Override
    public CompanyInfo getCompanyInfo(String code) {
        return dao.getCompanyInfo(code);
    }

    @Override
    public boolean checkCompany(String code) {
        return dao.checkCompany(code) > 0;
    }

    @Override
    public List<ChannelInfo> getChannelInfo(String code) {
        return dao.getAllChannelInfo(code);
    }

    @Override
    public List<ChannelInfo> getUnUseChannelInfo(String code) {
        return dao.getUnUseChannelInfo(code);
    }

    @Override
    public ResultResp deleteChannel(ChannelDeleteReq req) {
        if (req.getChannelList().isEmpty())
            return ResultResp.SUCCESS;
        dao.deleteChannel(req);
        return ResultResp.SUCCESS;
    }

    @Override
    public ResultResp addChannel(ChannelAddInfo req) {
        ChannelInfo channel = dao.getChannelInfoById(req.getCode(),req.getChannelId());
        if (channel != null)
            return new ResultResp("渠道已授权");

        if (dao.getChannelNum(req.getCode()) <= dao.countChannel(req.getCode()))
            return new ResultResp("授权数量不足。");
        dao.addChannel(req);
        return ResultResp.SUCCESS;
    }

    @Override
    public ResultResp updateChannel(ChannelUpdateReq req) {
        ChannelInfo channel = dao.getChannelInfoById(req.getCompanyCode(),req.getChannelId());
        if (channel == null)
            return new ResultResp("渠道不存在");
        channel.setBeginDate(req.getBeginDate());
        channel.setEndDate(req.getEndDate());
        if (dao.updateChannel(channel)>0)
            return ResultResp.SUCCESS;
        else
            return new ResultResp("修改失败");
    }
}
